﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion

using System;
using System.Collections;
using System.Linq;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PentaBiz.Cloud.CloudDb.EntityModel.SystemModel;

namespace PentaBiz.Cloud.Services.MetaService.Model.Testing
{
    [TestClass]
    public class UnitTestMetaDbQueryContext
    {
        [TestMethod]
        [TestCategory("MetaDbQueryContext")]
        public void TestIfCanCreateMetaDbQueryContext()
        {
            var m = new MetaDbQueryContext();

            Assert.IsNotNull(m);
        }

        [TestMethod]
        [TestCategory("MetaDbQueryContext")]
        public void TestIfAllQueryCountIsZero()
        {
            var m = new MetaDbQueryContext();

            m.LockContext();

            var props =
                typeof (MetaDbQueryContext).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p =>
                                                                                                             typeof (
                                                                                                                 IQueryable
                                                                                                                 )
                                                                                                                 .IsAssignableFrom
                                                                                                                 (
                                                                                                                     p
                                                                                                                         .PropertyType));

            foreach (var propertyInfo in props.ToArray())
            {
                Console.WriteLine("Found QueryFace: {0}", propertyInfo.PropertyType.GetGenericArguments()[0]);

                var queryable = (IQueryable) propertyInfo.GetValue(m, null);

                //get the QueryFace
                var o = (IEnumerable) propertyInfo.GetValue(m, null);

                //loop in Query
                var e = o.GetEnumerator();
                while (e.MoveNext())
                {
                    var item = e.Current;

                    Console.WriteLine("Selected {0}", item);
                    Assert.Fail("Selected {0}", item);
                    //break after first item
                }
            }

            Assert.IsNotNull(m);
        }

        [TestMethod]
        [TestCategory("MetaDbQueryContext")]
        public void TestIfAllQueryIsWorking()
        {
            var m = new MetaDbQueryContext();

            m.LoginUser(SystemScope.RootScopeName, SystemUser.RootServiceUserName, "secret");

            //get IQueryable properties
            var props =
                typeof (MetaDbQueryContext).GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p =>
                                                                                                             typeof (
                                                                                                                 IQueryable
                                                                                                                 )
                                                                                                                 .IsAssignableFrom
                                                                                                                 (
                                                                                                                     p
                                                                                                                         .PropertyType));

            //now for all Query faces
            foreach (var propertyInfo in props.ToArray())
            {
                //get the QueryFace
                var o = (IEnumerable) propertyInfo.GetValue(m, null);

                //loop in Query
                var e = o.GetEnumerator();
                while (e.MoveNext())
                {
                    var item = e.Current;

                    Console.WriteLine("Selected {0}", item);
                    //break after first item
                    break;
                }
            }

            Assert.IsNotNull(m);
        }
    }
}